package com.pmsystem.controller;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.Remove;
import com.pmsystem.pojo.User;
import com.pmsystem.service.AdministratorService;
import com.pmsystem.service.UserService;
import com.pmsystem.util.BeanUtil;


@Controller
@RequestMapping("admin")
public class AdministratorController {

	@Autowired
	private AdministratorService administratorService;
	@Autowired
	private UserService userService;

	/**
	 * 
	 * <p>Title: login</p >  
	 * <p>Description: 二级管理员登陆</p >  
	 * @author 泽强
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request,String username,String password) {
		Administrator admin = null;
		admin = administratorService.login(username,password);
		if (admin != null) {
			Byte enable=admin.getEnable();
			if(enable==1) {
				request.getSession().setAttribute("admin", admin);
				return "admin/index";
			}
			request.setAttribute("msg", "账号不可用");
		}else {
			request.setAttribute("msg", "账号或密码错误");
		}
		return "admin/login";
	}

	
	/**
	 * 
	 * <p>Title: login</p>
	 * <p>Description:  二级管理员退出</p> 
	 * @author 泽强
	 * @param session
	 * @param admin
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.invalidate();
		return "admin/login";
	}
	
	/**
	 * 
	 * <p>Title: update</p>
	 * <p>Description:  修改二级管理员信息</p> 
	 * @author 泽强
	 * @param admin
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@RequestParam Map<String,Object> map) {
		Administrator admin =BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Administrator.class);
		boolean bool = administratorService.updateAdministrator(admin);
		if(bool) {
			return "success";
		}
		return "error";
	}
	
	
	/**
	 * 
	 * <p>Title: findUserCheck</p>  
	 * <p>Description:查询已注册的合法用户的信息 </p>  
	 * @author 树昌
	 * @return
	 */
	@RequestMapping(value = "findUserCheck", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findUserCheck(String keyword){
		
		Map<String, Object> map = new HashMap<>();
		List<User> listuser =  administratorService.findUser(keyword);
		map.put("listuser", listuser);
		int size = listuser.size();
		map.put("size", size);
		return map;
	}
	
	
	
	/**
	 * <p>Title: findUserUncheck</p >  
	 * <p>Description:查询所有待审核用户信息</p >  
	 * @author 魏家洛
	 * @return
	 */
	@RequestMapping("findUserUncheck")
	@ResponseBody
	public List<User> findUserUncheck(){
		return administratorService.findUserUncheck();
	}
	/**
	 * <p>Title: passUser</p >  
	 * <p>Description:通过业主注册账号的审核</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return
	 */
	@RequestMapping("passUser")
	@ResponseBody
	public boolean passUser(String userId) {
		return administratorService.passUser(userId);
	}
	
	
	/**
	 * <p>Title: deleteUser</p >  
	 * <p>Description:删除用户或不通过业主账号注册申请</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "deleteUser" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteUser(int id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.deleteUser(id);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: deleteAllUser</p>  
	 * <p>Description:批量删除业主用户 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteAllUser" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllUser(int[] id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		boolean row = administratorService.deleteAllUser(listID);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: deleteAllUser</p>  
	 * <p>Description:批量通过业主用户 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "checkedAllUser" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkedAllUser(int[] id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		boolean row = administratorService.checkedAllUser(listID);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	
	/**
	 * 
	 * <p>Title: findUserByUserID</p>  
	 * <p>Description:根据业主ID查询业主信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "findUserByUserID" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findUserByUserID(int id){
		
		Map<String, Object> map = new HashMap<>();
		User user = administratorService.findUserByUserId(id);
		map.put("user", user);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: adminUpdateUserInfo</p>  
	 * <p>Description:修改用户信息 </p>  
	 * @author 树昌
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "adminUpdateUserInfo" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> adminUpdateUserInfo(User user){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.updateUser(user);
		if(row) {
			msg = "success";
		}else {
			msg ="error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: adminUpdateUserPassword</p>  
	 * <p>Description:管理员修改业主密码 </p>  
	 * @author 树昌
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "adminUpdateUserPassword" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> adminUpdateUserPassword(User user){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.updateUserPassword(user);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showAllAdmin</p>  
	 * <p>Description:查询所有的管理员账号 </p>  
	 * @author 树昌
	 * @param keyword
	 * @return
	 */
	@RequestMapping(value = "showAllAdmin" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showAllAdmin(String keyword){
		
		Map<String, Object> map = new HashMap<>();
		List<Administrator> listAdmin = administratorService.showAllAdmin(keyword);
		map.put("listAdmin", listAdmin);
		int size = listAdmin.size();
		map.put("size", size);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAdmin</p>  
	 * <p>Description:删除一个管理员账号 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteAdmin" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAdmin(int id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.deleteOneAdmin(id);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllAdmin</p>  
	 * <p>Description:批量删除管理员账号 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteAllAdmin" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllAdmin(int[] id){
		
		Map<String, Object> map = new HashMap<>();
		String msg =null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		boolean row = administratorService.deleteAllAdmin(listID);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: addAdminacc</p>  
	 * <p>Description:添加管理员账号 </p>  
	 * @author 树昌
	 * @param administrator
	 * @return
	 */
	@RequestMapping(value = "addAdminacc" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAdminacc(Administrator administrator){
		Map<String, Object> map = new HashMap<>();
		String msg =null;
		boolean row = administratorService.addAdminacc(administrator);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: findAdminbyID</p>  
	 * <p>Description:根据ID查询一条管理员账号信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "findAdminbyID" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findAdminbyID(int id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		Administrator administrator = administratorService.findAdminByID(id);
		map.put("admin", administrator);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: updateAdminInfo</p>  
	 * <p>Description:修改管理员信息 </p>  
	 * @author 树昌
	 * @param administrator
	 * @return
	 */
	@RequestMapping(value = "updateAdminInfo" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateAdminInfo(Administrator administrator){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.updateAdministrator(administrator);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: updateAdminPassword</p>  
	 * <p>Description:修改管理员密码 </p>  
	 * @author 树昌
	 * @param administrator
	 * @return
	 */
	@RequestMapping(value = "updateAdminPassword" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateAdminPassword(Administrator administrator){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.updateAdminPassword(administrator);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showRemove</p>  
	 * <p>Description:查询所有放在垃圾箱的账户 </p>  
	 * @author 树昌
	 * @param keyword
	 * @return
	 */
	@RequestMapping(value = "showRemove" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showRemove(String keyword){
		
		Map<String, Object> map = new HashMap<>();
		List<Remove> listRemove = administratorService.showRemove(keyword);
		map.put("listRemove", listRemove);
		int size = listRemove.size();
		map.put("size", size);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: recoveryAcc</p>  
	 * <p>Description:将账号从垃圾箱恢复使用 </p>  
	 * @author 树昌
	 * @param AccID
	 * @param isAdmin
	 * @return
	 */
	@RequestMapping(value = "recoveryAcc" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> recoveryAcc(int AccID ,int isAdmin){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row =administratorService.recoveryAcc(AccID, isAdmin);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: permanentDelete</p>  
	 * <p>Description:永久性删除账号 </p>  
	 * @author 树昌
	 * @param AccID
	 * @param isAdmin
	 * @return
	 */
	@RequestMapping(value = "permanentDelete" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> permanentDelete(int AccID ,int isAdmin){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row =administratorService.permanentDelete(AccID, isAdmin);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showRegist</p>  
	 * <p>Description:显示申请注册的用户 </p>  
	 * @author 树昌
	 * @param keyword
	 * @return
	 */
	@RequestMapping(value = "showRegist" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showRegist(String keyword){
		
		Map<String, Object> map = new HashMap<>();
		List<User> listUser = administratorService.showRegist(keyword);
		map.put("listUser", listUser);
		int size =listUser.size();
		map.put("size", size);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: userCheck</p>  
	 * <p>Description:管理员通过业主注册申请 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "userCheck" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userCheck(int id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = administratorService.userChecked(id);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	
}
